<?php


namespace App\Http\Controllers\Order;


use App\Http\Controllers\Controller;
use App\Http\Requests\Order\Index\StoreRequest;
use App\Http\Requests\Order\Index\UpdateRequest;
use App\Http\Searchs\Order\IndexSearch;
use App\Repositories\Order;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * @var Order
     * @author luffyzhao@vip.126.com
     */
    private $order;

    /**
     * IndexController constructor.
     * @param Order $order
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * @param IndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->order->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->order->create($request->only([
                    'number', 'customer_id', 'express_id', 'freight', 'buyer_name', 'receiving_name', 'receiving_phone', 'buyer_type', 'buyer_number', 'buyer_telephone', 'areas', 'address', 'remark', 'products', 'original_number', 'ebc_code', 'ebc_name','ebp_code', 'ebp_name',
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit($id){
        return $this->response(
            $this->order->edit($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($request, $id) {
                $this->order->update($id, $request->only([
                    'express_id', 'freight', 'buyer_name', 'receiving_name', 'receiving_phone', 'buyer_type', 'buyer_number', 'buyer_telephone', 'areas', 'address', 'remark', 'products', 'original_number', 'ebc_code', 'ebc_name','ebp_code', 'ebp_name',
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirm($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->order->confirm($id);
            })
        );
    }
}
